package com.ryder.subject.domain.handler.subject;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.ryder.subject.common.enums.IsDeleteFlagEnum;
import com.ryder.subject.common.enums.SubjectInfoTypeEnum;
import com.ryder.subject.domain.convert.JudgeSubjectConvert;
import com.ryder.subject.domain.entity.SubjectAnswerBO;
import com.ryder.subject.domain.entity.SubjectInfoBO;
import com.ryder.subject.domain.entity.SubjectOptionBO;
import com.ryder.subject.infra.basic.entity.SubjectBrief;
import com.ryder.subject.infra.basic.entity.SubjectJudge;
import com.ryder.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * 描述: 判断题目的策略类
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025/02/05
 */

@Component
public class JudgeTypeHandler implements SubjectTypeHandler{

    @Autowired
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        //只处理JUDGE相关的
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //判断题目的插入
        SubjectJudge subjectJudge = new SubjectJudge();
        if (subjectInfoBO == null) {
            throw new RuntimeException("参数错误");
        }
        Preconditions.checkArgument(CollectionUtils.isNotEmpty(subjectInfoBO.getOptionList()), "题目选项不能为空");

        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(Long.valueOf(subjectId));
        List<SubjectJudge> result = subjectJudgeService.queryByCondition(subjectJudge);
        List<SubjectAnswerBO> subjectAnswerBOList = JudgeSubjectConvert.INSTANCE.convertJudgeToBoList(result);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
